package cn.edu.scau.cmi.xuxingao.controller.composite;

import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xuxingao.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.xuxingao.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.xuxingao.composite.consistent.ConsistentLeaf;
import cn.edu.scau.cmi.xuxingao.composite.safe.SafeComponent;
import cn.edu.scau.cmi.xuxingao.composite.safe.SafeComposite;
import cn.edu.scau.cmi.xuxingao.composite.safe.SafeLeaf;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class CompositeController {
    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="safeModeBtn"
    private Button safeModeBtn; // Value injected by FXMLLoader

    @FXML // fx:id="consistentModeBtn"
    private Button consistentModeBtn; // Value injected by FXMLLoader

    @FXML // fx:id="getPersonTfd"
    private TextField getPersonTfd; // Value injected by FXMLLoader

    @FXML // fx:id="getPersonParentTfd"
    private TextField getPersonParentTfd; // Value injected by FXMLLoader

    @FXML // fx:id="addPersonBtn"
    private Button addPersonBtn; // Value injected by FXMLLoader

    @FXML // fx:id="getGroupTfd"
    private TextField getGroupTfd; // Value injected by FXMLLoader

    @FXML // fx:id="getGroupParentTfd"
    private TextField getGroupParentTfd; // Value injected by FXMLLoader

    @FXML // fx:id="addGroupBtn"
    private Button addGroupBtn; // Value injected by FXMLLoader

    @FXML // fx:id="displayTa"
    private TextArea displayTa; // Value injected by FXMLLoader

    @FXML // This method is called by the FXMLLoader when initialization is complete
    
    private String mode="";
    private final String safeModeString="Safe";
    private final String consistentModeString="Consistent";
    private SafeComponent root= new SafeComposite("所有人");
    private ConsistentComponent roott= new ConsistentComposite("所有人");
  //  private List<SafeComponent> root = new ArrayList<SafeComponent>();
    private String personName;
    private String personParent;
    private String groupName;
    private String groupParent;
    private String display;
    private String displayy;
    private SafeComponent temp;
    private ConsistentComponent tempp;
    
	public void contains(SafeComposite parent, String name) {
		Iterator<SafeComponent> iter = parent.getChildren().iterator();
		while(iter.hasNext()) {
			SafeComponent child = iter.next();
			if(child instanceof SafeComposite) {
				if(((SafeComposite) child).getName().equals(name))
					temp=(SafeComposite)child;
				else
					contains((SafeComposite)child,name);
			}	
//			if(child instanceof SafeComposite) {
//				if(((SafeComposite)child).getName().equals(name)) {
//					return (SafeComposite)child;
//				}
//			}
		}
	}
	
	public void containss(ConsistentComponent parent, String name) {
		if(parent.getChildren()!=null) {
		Iterator<ConsistentComponent> iter = parent.getChildren().iterator();
		while(iter.hasNext()) {
			ConsistentComponent child = iter.next();
				if(child.getName().equals(name))
					tempp=(ConsistentComposite)child;
				else
					containss(child,name);	
		}
		}
	}
	
    @FXML
    void safeMode(ActionEvent e) {
    	mode=safeModeString;
    }
    
    @FXML
    void consistentMode(ActionEvent e) {
    	mode=consistentModeString;
    }
    
    @FXML 
    void addPerson(ActionEvent e){
    	switch(mode) {
    	case safeModeString:{
        		personName = getPersonTfd.getText();
        		personParent = getPersonParentTfd.getText();
        		if(personParent.equals(""))
        			((SafeComposite)root).getChildren().add(new SafeLeaf(personName));
        		else {
        			contains((SafeComposite)root,personParent);
        			if(temp!=null) {
        				((SafeComposite)temp).getChildren().add(new SafeLeaf(personName));
        				temp=null;
        			}
        			else {
        				System.out.println("不存在团队，需新建\n");
        				SafeComposite newGroup = new SafeComposite(personParent);
        				newGroup.getChildren().add(new SafeLeaf(personName));
        				((SafeComposite)root).getChildren().add(newGroup);
        				
        			}
        		}	

    		getPersonTfd.clear();
        	getPersonParentTfd.clear();
        	display=root.intro(0);
        	displayTa.setText(display);
        	break;
    		
    	}
    	case consistentModeString:{
    		personName = getPersonTfd.getText();
    		personParent = getPersonParentTfd.getText();
    		if(personParent.equals(""))
    			((ConsistentComposite)roott).getChildren().add(new ConsistentLeaf(personName));
    		else {
    			containss((ConsistentComposite)roott,personParent);
    			if(tempp!=null) {
    				((ConsistentComposite)tempp).getChildren().add(new ConsistentLeaf(personName));
    				tempp=null;
    			}
    			else {
    				System.out.println("不存在团队，需新建\n");
    				ConsistentComposite newGroup = new ConsistentComposite(personParent);
    				newGroup.getChildren().add(new ConsistentLeaf(personName));
    				((ConsistentComposite)roott).getChildren().add(newGroup);
    				
    			}
    		}	

		getPersonTfd.clear();
    	getPersonParentTfd.clear();
    	displayy=roott.intro(0);
    	displayTa.setText(displayy);
    	break;
    		
    	}
    	default: displayTa.setText("请选择组合模式的设计形式！");
    	}
    	
    }
    
    @FXML
    void addGroup(ActionEvent e) {
    	switch(mode){
    	case safeModeString:{
    		groupName = getGroupTfd.getText();
    		groupParent = getGroupParentTfd.getText();
    		if(groupParent.equals(""))
    			((SafeComposite)root).getChildren().add(new SafeComposite(groupName));
    		else {
    			contains((SafeComposite)root,groupParent);
    			if(temp!=null) {
        			((SafeComposite)temp).getChildren().add(new SafeComposite(groupName));
        			temp=null;
    			}
    			else {
    				System.out.println("不存在团队父亲，需新建\n");
    				SafeComposite newGroupParent = new SafeComposite(groupParent);
    				newGroupParent.getChildren().add(new SafeComposite(groupName));
    				((SafeComposite)root).getChildren().add(newGroupParent);
    			}

    		}
    		getGroupTfd.clear();
        	getGroupParentTfd.clear();
        	display=root.intro(0);
        	displayTa.setText(display);	
        	break;
    	}
    	case consistentModeString: {
    		groupName = getGroupTfd.getText();
    		groupParent = getGroupParentTfd.getText();
    		if(groupParent.equals(""))
    			((ConsistentComposite)roott).getChildren().add(new ConsistentComposite(groupName));
    		else {
    			containss((ConsistentComposite)roott,groupParent);
    			if(tempp!=null) {
        			((ConsistentComposite)tempp).getChildren().add(new ConsistentComposite(groupName));
        			tempp=null;
    			}
    			else {
    				System.out.println("不存在团队父亲，需新建\n");
    				ConsistentComposite newGroupParent = new ConsistentComposite(groupParent);
    				newGroupParent.getChildren().add(new ConsistentComposite(groupName));
    				((ConsistentComposite)roott).getChildren().add(newGroupParent);
    			}

    		}
    		getGroupTfd.clear();
        	getGroupParentTfd.clear();
        	displayy=roott.intro(0);
        	displayTa.setText(displayy);	
        	break;
    	}
    	default:  displayTa.setText("请选择组合模式的设计形式！");
    	}
    	
    }
    
    void initialize() {
        assert safeModeBtn != null : "fx:id=\"safeModeBtn\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert consistentModeBtn != null : "fx:id=\"consistentModeBtn\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert getPersonTfd != null : "fx:id=\"getPersonTfd\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert getPersonParentTfd != null : "fx:id=\"getPersonParentTfd\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert addPersonBtn != null : "fx:id=\"addPersonBtn\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert getGroupTfd != null : "fx:id=\"getGroupTfd\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert getGroupParentTfd != null : "fx:id=\"getGroupParentTfd\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert addGroupBtn != null : "fx:id=\"addGroupBtn\" was not injected: check your FXML file 'CompositeController.fxml'.";
        assert displayTa != null : "fx:id=\"displayTa\" was not injected: check your FXML file 'CompositeController.fxml'.";

    }
}
